package com.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import com.tanhua.autoconfig.properties.AipfaceProperties;
import org.json.JSONObject;

import java.util.HashMap;

public class AipfaceTemplate {

    private AipfaceProperties properties;

    public AipfaceTemplate(AipfaceProperties properties) {
        this.properties = properties;
    }

    public boolean detect(String imageUrl) {
        AipFace client = new AipFace(properties.getAPP_ID(), properties.getAPI_KEY(), properties.getSERET_KEY());
        // 调用接口
        String imageType = "URL";

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        System.out.println(res.toString(2));

        Integer error_code = (Integer) res.get("error_code");

        return error_code == 0;
    }
}
